﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using System.Data;

namespace Warehouse
{
    class UpdateVersion
    {
        private string _server;
        private string _database;
        private string _uid;
        private string _password;
        private string _connectionString;
        private MySqlConnection _connection;

        public UpdateVersion()
        {
            Initialize();
        }
        private void Initialize()
        {
        }

        string _version = "";
        public void Update()
        {
            _server = "localhost";
            _database = "pusat";
            _uid = "root";
            _password = "";
            _connectionString = "SERVER=" + _server + ";" + "DATABASE=" +
            _database + ";" + "UID=" + _uid + ";" + "PASSWORD=" + _password + ";";
            _connection = new MySqlConnection(_connectionString);

            MySqlCommand _cmd = OpenConnection().CreateCommand();
            _cmd.CommandText = "SELECT Version FROM version LIMIT 0,1";
            MySqlDataAdapter adap = new MySqlDataAdapter(_cmd);
            DataSet ds = new DataSet();
            adap.Fill(ds);

            DataTable myDataTable = ds.Tables[0];
            DataRow tempRow = null;
            foreach (DataRow tempRow_Variable in myDataTable.Rows)
            {
                tempRow = tempRow_Variable;
                _version = tempRow["Version"].ToString();
            }
            CloseConnection();

            if (_version != getCurrentVersion())
            {
                _cmd = OpenConnection().CreateCommand();
                _cmd.CommandText = "UPDATE version SET Version = '" + _version + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
            }
        }

        private String getCurrentVersion()
        {
            string _text = _version;
            DBConnect _db = new DBConnect("localhost", "warehouse_cimahi");
            _connection = new MySqlConnection(_db.getDBString());
            MySqlCommand cmd = OpenConnection().CreateCommand();
            cmd.CommandText = "SELECT Version FROM version LIMIT 0,1";
            MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adap.Fill(ds);

            DataTable myDataTable = ds.Tables[0];
            DataRow tempRow = null;
            foreach (DataRow tempRow_Variable in myDataTable.Rows)
            {
                tempRow = tempRow_Variable;
                _text = tempRow["Version"].ToString();
            }
            CloseConnection();
            return _text;
        }

        public MySqlConnection OpenConnection()
        {
            try
            {
                _connection.Open();
                return _connection;
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to server.  Contact administrator");
                        break;

                    case 1045:
                        MessageBox.Show("Invalid username/password, please try again");
                        break;
                }
                return _connection;
            }
        }

        public bool CloseConnection()
        {
            try
            {
                _connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public string getDBString() { return _connectionString; }
        public MySqlConnection getConnection() { return _connection; }
    }
}
